<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class CollectController extends HomeController {
 	//xuxiang
// 	public function _initialize(){
		
//  		parent::_initialize();
// 	}
	/**
	 * 添加收藏
	 */
	public function setCollect(){
		//是否登录
		//获取商品id
		//添加收藏
		$open_id = session('openid');
		$goods_id = I('goods_id');
		
		if(empty($goods_id)){
			$this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍后再试'],'JSONP');
		}
		
		$is_shoucang = M('Collect')->where(array('goods_id'=>$goods_id,'open_id'=>$open_id))->find();
		if($is_shoucang){
		    $re = M('Collect')->where(array('goods_id'=>$goods_id,'open_id'=>$open_id))->delete();
		}else{
		    $data['open_id'] = $open_id;
		    $data['goods_id'] = $goods_id;
		    $data['add_time'] = time();
		    $re = M('Collect')->add($data);
		}
		if($re){
			$this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
		}else{
			$this->ajaxReturn(['status'=>-2,'msg'=>'系统繁忙，请稍候再试'],'JSONP');
		}
	}
	/**
	 * 收藏列表
	 */
	public function CollectList(){
		//是否登录
		//列出收藏列表
		$open_id = session('openid');
		$list = M('Collect c')
		->join('yang_goods g ON c.goods_id = g.goods_id')
		->join('yang_goods_attr ga ON ga.goods_id = g.goods_id')
		->where(array('open_id'=>$open_id))->select();
		/* foreach($list as $k=>$v){
			$list[$k]['goods'] = M('Goods g')
		                          ->join('yang_goods_attr ga ON ga.goods_id = g.goods_id','left')
		                          ->where(array('g.goods_id'=>$v['goods_id']))->find(); 
		} */
		$this->ajaxReturn($list,'JSONP');
	}
	
	/**
	 * 删除收藏
	 */
	public function delCollectById(){
		$open_id = session('openid');
		$collect_id = I('collect_id');
		if(empty($collect_id)){
			$this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍候再试'],'JSONP');
		}	
		$re = M('Collect')->where(array('collect_id'=>$collect_id))->delete();
		if($re){
			$this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
		}else{
			$this->ajaxReturn(['status'=>-2,'msg'=>'系统繁忙，请稍候再试'],'JSONP');
		}
	}
}